import os,re
os.environ['DJANGO_SETTINGS_MODULE'] = 'drf2.settings'
import django
django.setup()
from aaa import models
from django.db.models import Sum

goodsObj = models.Goods.objects.get(goods_id=10022)
print(goodsObj)
x = models.Params.objects.filter(attr_sel=1,cat_id_id=goodsObj.goods_cat)

# goodsproducts = models.GoodsProduct.objects.filter(goods=goodsObj)
# n = goodsproducts.aggregate(number_sum=Sum('number'))['number_sum']
# # n = round((goodsObj.goods_price/goodsObj.ori_price)*10, 1)
# print(n)

goodsspecifications = []
for i in x:
    for val in i.attr_vals.split(' '):
        speci_dict = {}
        speci_dict['specification'] = i.attr_name
        speci_dict['value'] = val
        goodsspecifications.append(speci_dict)

print(goodsspecifications)

goodsspecification_list = []
specification_name_list = []
for item in goodsspecifications:
    temp_dic = {}
    name_temp = item['specification']
    if name_temp in specification_name_list:
        index_arr = specification_name_list.index(name_temp)
        goodsspecification_list[index_arr]['values'].append({"checked": False, "value": item['value']})
    else:
        specification_name_list.append(name_temp)
        temp_list = []
        temp_list.append({"checked": False, "value": item['value']})
        temp_dic['name'] = name_temp
        temp_dic['values'] = temp_list
        goodsspecification_list.append(temp_dic)

print(goodsspecification_list)